<template>
  <div class="company-info-container">
    <h2>企业信息</h2>
    
    <el-card class="info-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>企业基本信息</span>
          <el-button type="primary" size="small" @click="handleEdit">
            {{ isEditing ? '保存' : '编辑' }}
          </el-button>
        </div>
      </template>
      
      <div v-if="companyInfo">
        <el-form ref="companyFormRef" :model="companyInfo" :disabled="!isEditing" label-width="120px">
          <el-form-item label="公司名称">
            <el-input v-model="companyInfo.companyName" />
          </el-form-item>
          
          <el-form-item label="公司地址">
            <el-input v-model="companyInfo.companyAddress" />
          </el-form-item>
          
          <el-form-item label="公司类型">
            <el-select v-model="companyInfo.companyType" placeholder="请选择公司类型" style="width: 100%">
              <el-option label="有限责任公司" value="有限责任公司" />
              <el-option label="股份有限公司" value="股份有限公司" />
              <el-option label="个人独资企业" value="个人独资企业" />
              <el-option label="合伙企业" value="合伙企业" />
              <el-option label="其他" value="其他" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      
      <el-empty v-else-if="!loading" description="暂无企业基本信息"></el-empty>
    </el-card>
    
    <el-card class="info-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>法人信息</span>
        </div>
      </template>
      
      <div v-if="companyInfo">
        <el-form ref="legalFormRef" :model="companyInfo" :disabled="!isEditing" label-width="120px">
          <el-form-item label="法人姓名">
            <el-input v-model="companyInfo.legalName" />
          </el-form-item>
          
          <el-form-item label="联系方式">
            <el-input v-model="companyInfo.legalPhone" />
          </el-form-item>
          
          <el-form-item label="身份证号">
            <el-input v-model="companyInfo.legalIdCard" />
          </el-form-item>
        </el-form>
      </div>
      
      <el-empty v-else-if="!loading" description="暂无法人信息"></el-empty>
    </el-card>
    
    <el-card class="info-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>账户信息</span>
        </div>
      </template>
      
      <div v-if="companyInfo">
        <el-form ref="accountFormRef" :model="companyInfo" :disabled="!isEditing" label-width="120px">
          <el-form-item label="银行账户">
            <el-input v-model="companyInfo.bankAccount" />
          </el-form-item>
        </el-form>
        
        <div class="account-info">
          <p><strong>用户名：</strong> {{ username }}</p>
          <p><strong>用户类型：</strong> 企业用户</p>
          <p><strong>注册时间：</strong> {{ formatDate(companyInfo.createTime) }}</p>
        </div>
      </div>
      
      <el-empty v-else-if="!loading" description="暂无账户信息"></el-empty>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getCompanyInfo, updateCompanyInfo } from '@/api/company'
import { formatDate } from '@/utils/format'

const username = ref(localStorage.getItem('username') || '用户')
const loading = ref(false)
const companyInfo = ref(null)
const isEditing = ref(false)

const companyFormRef = ref(null)
const legalFormRef = ref(null)
const accountFormRef = ref(null)

onMounted(() => {
  loadCompanyInfo()
})

const loadCompanyInfo = async () => {
  loading.value = true
  try {
    const response = await getCompanyInfo()
    companyInfo.value = response.data
  } catch (error) {
    console.error('加载企业信息失败:', error)
    ElMessage.error('获取企业信息失败')
  } finally {
    loading.value = false
  }
}

const handleEdit = async () => {
  if (isEditing.value) {
    // 保存企业信息
    try {
      await updateCompanyInfo(companyInfo.value)
      ElMessage.success('企业信息更新成功')
      isEditing.value = false
    } catch (error) {
      console.error('更新企业信息失败:', error)
      ElMessage.error('更新企业信息失败')
    }
  } else {
    // 进入编辑模式
    isEditing.value = true
  }
}
</script>

<style scoped>
.company-info-container {
  padding: 20px;
}

.info-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.account-info {
  margin-top: 20px;
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.account-info p {
  line-height: 2;
  margin: 0;
}
</style> 